<template>
	<view class="root-wrap">
		<view class="form-item">
			<view class="label">
				<text class="title">收款户名</text>
				<text v-if="!readOnly" class="require">*</text>
			</view>
			<view class="input-wrap">
				<u-input
					placeholder="请输入收款户名"
					border="none"
					v-model="userInfo.account"
					fontSize="28rpx"
					:readonly="readOnly"
					@change="e=>change(e,'account')"
				  ></u-input>
			</view>
		</view>
		<view class="form-item">
			<view class="label">
				<text class="title">银行帐号</text>
				<text v-if="!readOnly" class="require">*</text>
			</view>
			<view class="input-wrap">
				<u-input
					placeholder="请输入银行帐号"
					border="none"
					v-model="userInfo.bank_no"
					fontSize="28rpx"
					:readonly="readOnly"
					@change="e=>change(e,'bank_no')"
				  ></u-input>
			</view>
		</view>
		<view class="form-item">
			<view class="label">
				<text class="title">开户网点</text>
				<text v-if="!readOnly" class="require">*</text>
			</view>
			<view class="input-wrap">
				<u-input
					placeholder="请输入开户网点"
					border="none"
					v-model="userInfo.open_address"
					fontSize="28rpx"
					:readonly="readOnly"
					@change="e=>change(e,'open_address')"
				  ></u-input>
			</view>
		</view>
		
		<view class="form-item">
			<view class="label">
				<text class="title">所属银行</text>
				<text v-if="!readOnly" class="require">*</text>
			</view>
			<view class="input-wrap">
				<u-input
					placeholder="请输入所属银行"
					border="none"
					v-model="userInfo.bank_name"
					fontSize="28rpx"
					:readonly="readOnly"
					@change="e=>change(e,'bank_name')"
				  ></u-input>
			</view>
		</view>
		<view class="form-item">
			<view class="label">
				<text class="title">银行联行号</text>
				<text v-if="!readOnly" class="require">*</text>
			</view>
			<view class="input-wrap">
				<u-input
					placeholder="请输入银行联行号"
					border="none"
					v-model="userInfo.bank_code"
					fontSize="28rpx"
					:readonly="readOnly"
					@change="e=>change(e,'bank_code')"
				  ></u-input>
			</view>
		</view>
		<view class="submit"><u-button @click="onSubmit" color="#30D6C2" type="primary" shape="circle">保存<image class="icon" src="../../../static/assets/imgs/next-step.png"></image></u-button></view>
	</view>
</template>

<script>
	import { updateBankInfo,getUserInfo } from '@/api/user.js';
	export default {
		data() {
			return {
				userInfo: {},
				loading: false,
				readOnly: false,
			};
		},
		onLoad(){
			this.userInfo = this.vuex_user;
			this.getUserInfo();
		},
		methods:{
			getUserInfo(){
				getUserInfo().then((res)=>{
					if (1 == res.code) {
						this.userInfo = res.data;
						this.$u.vuex('vuex_user', res.data)
					} else {
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				})
			},
			change(e,key){
				this.userInfo[key] = e;
			},
			onSubmit(){
				if(this.loading) return;
				const { id,account,bank_no,open_address,bank_name,bank_code } = this.userInfo;
				
				if(!account || !bank_no || !open_address || !bank_name || !bank_code){
					uni.showToast({
						title:'请检查所有必填项',
						icon:'none'
					})
					return;
				}
				this.loading = true;
				uni.showLoading({
					title: '提交中...'
				})
				const params = {
					"vendor_id": id, //供应商ID
					"account": account, //收款户名
					"bank_no": bank_no, //银行帐号
					"open_address": open_address, //开户网点
					"bank_name": bank_name, //所属银行
					"bank_code": bank_code //银行联行号
				}
				updateBankInfo(params).then(res => {
					this.loading = false;
					uni.hideLoading();
					if(res.code==1){
						uni.showToast({
							title:'提交成功',
							icon:'success'
						})
						const timer = setTimeout(()=>{
							uni.navigateBack({
								delta: 1
							})
							clearTimeout(timer);
						},900);
					}else {
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
.root-wrap{
	padding-bottom: 110rpx;
	.form-item{
		margin: 0 24rpx;
		.label{
			display: flex;
			justify-content: flex-start;
			align-items: center;
			margin: 44rpx 0 24rpx;
			.title{
				font-size: 28rpx;
				color: #606266;
				line-height: 50rpx;
			}
			.require{
				font-size: 28rpx;
				color: #E62020;
				line-height: 50rpx;
				margin-left: 24rpx;
			}
		}
		.input-wrap{
			width: 700rpx;
			height: 90rpx;
			background-color: #F3F4F6;
			margin: 0 auto;
			border-radius: 8rpx;
			.u-input{
				height: 100% !important;
				input{
					padding: 0 24rpx !important;
				}
			}
		}
	}
	.submit{
		width: 208rpx;
		height: 80rpx;
		margin: 88rpx auto 110rpx;
		.icon{
			width: 48rpx;
			height: 48rpx;
			margin-left: 16rpx;
		}
		.label{
			font-weight: 500;
			font-size: 32rpx;
			color: #fff;
		}
	}
	.u-input__content__subfix-icon{
		margin-right: 16rpx;
	}
}
</style>
